package com.zlm.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

/**
 * @author zlm
 * @date 2025/4/27
 */
public class TestMethod {

    public static void main(String[] args) throws Exception {
        Class<?> aClass = Class.forName("com.zlm.reflect.Student");
        //testGetMethods(aClass);
        testGetMethod(aClass);
    }

    public static void testGetMethods(Class<?> aClass)throws Exception{
        Method[] declaredMethods = aClass.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println("方法名字"+declaredMethod.getName());
            System.out.println("方法返回值类型"+declaredMethod.getReturnType());
            System.out.println("方法参数个数"+declaredMethod.getParameterCount());
        }
    }

    public static void testGetMethod(Class<?> aClass)throws Exception{
        Method eatMethod = aClass.getDeclaredMethod("eat");
        eatMethod.setAccessible(true);
        Constructor<?> constructor = aClass.getDeclaredConstructor();
        constructor.setAccessible(true);
        Object obj = constructor.newInstance();
        eatMethod.invoke(obj);

    }
}
